package com.ian.util;

import com.ian.constant.Constants;

import java.math.BigDecimal;
import java.util.Random;
import java.util.UUID;

/**
 * String工具类
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {
    /**
     * 根据传递过来的参数capacity，获取指定长度的随机字符串（方法1）
     *
     * @param capacity
     * @return String
     */
    public static String getRandomStr(int capacity) {
        Random random = new Random();
        int len = Constants.RANDOM_STR_ARR.length;
        StringBuilder sb = new StringBuilder(capacity);
        for (int i = 0; i < capacity; i++) {
            String ch = Constants.RANDOM_STR_ARR[random.nextInt(len)];
            sb.append(ch);
        }
        return sb.toString();
    }

    /**
     * 根据传递过来的参数length，获取指定长度的随机字符串（方法2）
     *
     * @param length
     * @return String
     */
    public static String getRandomStr2(int length) {
        //1.  定义一个字符串（A-Z，a-z，0-9）即62个数字字母；
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789*!@#$%^&*()_+<>?";
        int strLen = str.length();
        //2.  由Random生成随机数
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        //3.  长度为几就循环几次
        for (int i = 0; i < length; ++i) {
            //从62个的数字或字母中选择
            int number = random.nextInt(strLen);
            //将产生的数字通过length次承载到sb中
            sb.append(str.charAt(number));
        }
        //将承载的字符转换成字符串
        return sb.toString();
    }

    /**
     * 生成32位随机字符串，小写字母a - f，和数字组成（方法3）
     *
     * @return String
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().trim().replaceAll("-", "");
    }

    /**
     * 获取指定长度的随机字符串（方法4）：
     * commons-lang.jar包下有一个RandomStringUtils类，
     *  (1)其中有一个randomAlphanumeric(int length)函数，可以随机生成一个长度为length的字符串
     *      RandomStringUtils.randomAlphanumeric(32)
     *  (2)RandomStringUtils.random(32,true,false) 获取包含字母、不含数字的32位随机字符串
     */

    /**
     * 校验入参是否为一个 url
     *
     * @param str
     * @return
     */
    public static boolean isUrl(String str) {
        if (isBlank(str)) {
            return false;
        }
        str = str.trim();
        return str.matches("^(http|https)://.+");
    }

    public static String convertToDownloadSpeed(BigDecimal bigDecimal, int scale) {
        BigDecimal unit = new BigDecimal(1);
        BigDecimal kb = new BigDecimal(1 << 10);
        BigDecimal mb = new BigDecimal(1 << 10).multiply(kb);
        BigDecimal gb = new BigDecimal(1 << 10).multiply(mb);
        BigDecimal tb = new BigDecimal(1 << 10).multiply(gb);
        BigDecimal pb = new BigDecimal(1 << 10).multiply(tb);
        BigDecimal eb = new BigDecimal(1 << 10).multiply(pb);
        if (bigDecimal.divide(kb, scale, BigDecimal.ROUND_HALF_UP).compareTo(unit) < 0)
            return bigDecimal.divide(unit, scale, BigDecimal.ROUND_HALF_UP).toString() + " B";
        else if (bigDecimal.divide(mb, scale, BigDecimal.ROUND_HALF_UP).compareTo(unit) < 0)
            return bigDecimal.divide(kb, scale, BigDecimal.ROUND_HALF_UP).toString() + " KB";
        else if (bigDecimal.divide(gb, scale, BigDecimal.ROUND_HALF_UP).compareTo(unit) < 0)
            return bigDecimal.divide(mb, scale, BigDecimal.ROUND_HALF_UP).toString() + " MB";
        else if (bigDecimal.divide(tb, scale, BigDecimal.ROUND_HALF_UP).compareTo(unit) < 0)
            return bigDecimal.divide(gb, scale, BigDecimal.ROUND_HALF_UP).toString() + " GB";
        else if (bigDecimal.divide(pb, scale, BigDecimal.ROUND_HALF_UP).compareTo(unit) < 0)
            return bigDecimal.divide(tb, scale, BigDecimal.ROUND_HALF_UP).toString() + " TB";
        else if (bigDecimal.divide(eb, scale, BigDecimal.ROUND_HALF_UP).compareTo(unit) < 0)
            return bigDecimal.divide(pb, scale, BigDecimal.ROUND_HALF_UP).toString() + " PB";
        return bigDecimal.divide(eb, scale, BigDecimal.ROUND_HALF_UP).toString() + " EB";
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        if ((len & 1) == 1) {
            s = "0" + s;
            len++;
        }
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }
}
